﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Web;
using System.Web.UI;
using Jumbot.Common;
using Jumbot.DataOper;
using JumbotCms.API;
using JumbotCms.DataObject;

namespace JumbotCms.API
{
    public class Mphoto : Marticle
    {
        public Mphoto()
        {
            site = (Site)System.Web.HttpContext.Current.Application["System:Info"];
        }
        public override void CreateContent(DbOperHandler doh, string _ChannelId, string _ContentId, int _CurrentPage)
        {
            doh.Reset();
            doh.SqlCmd = "SELECT [PhotoUrl],[PageSize] FROM [jcms_module_photo] WHERE [ChannelId]=" + _ChannelId + " and [Id]=" + _ContentId;
            DataTable dtContent = doh.GetDataTable();
            //图片地址分割处理
            string PhotoUrl = dtContent.Rows[0]["PhotoUrl"].ToString().Replace("\r\n", "\r").Replace("|||", "\r");
            int PSize = Str2Int(dtContent.Rows[0]["PageSize"].ToString(), 1);
            dtContent.Clear();
            dtContent.Dispose();
            if (PhotoUrl != "")
            {
                int pageCount = 1;
                string[] PhotoUrlArr = PhotoUrl.Split(new string[] { "\r" }, StringSplitOptions.RemoveEmptyEntries);
                if (PhotoUrlArr.Length >= PSize)
                {
                    if (PhotoUrlArr.Length % PSize == 0)
                        pageCount = PhotoUrlArr.Length / PSize;
                    else
                        pageCount = (PhotoUrlArr.Length / PSize) + 1;
                }
                for (int j = 1; j < (pageCount + 1); j++)
                {
                    Jumbot.Common.DirFile.SaveFile(GetContent(doh, _ChannelId, _ContentId, j), Go2View(doh, j, true, _ChannelId, _ContentId));
                }
                doh.Reset();
                doh.SqlCmd = "UPDATE [jcms_module_photo] SET [OutUrl]='" + Go2View(doh, 1, true, _ChannelId, _ContentId) + "' WHERE [ChannelId]=" + _ChannelId + " and [IsPass]=1 and [Id]=" + _ContentId;
                doh.ExecuteSqlNonQuery();
            }
        }
        public override string GetContent(DbOperHandler doh, string _ChannelId, string _ContentId, int _CurrentPage)
        {
            doh.Reset();
            doh.ConditionExpress = "id=@id and Enabled=1";
            doh.AddConditionParameter("@id", _ChannelId);
            if (!doh.Exist("jcms_normal_channel"))
            {
                return "";
            }
            doh.Reset();
            doh.SqlCmd = "SELECT [ClassId] FROM [jcms_module_photo] WHERE [ChannelId]=" + _ChannelId + " and [IsPass]=1 and [Id]=" + _ContentId;
            DataTable dtSearch = doh.GetDataTable();
            if (dtSearch.Rows.Count == 0)
            {
                dtSearch.Clear();
                dtSearch.Dispose();
                return "";
            }
            string ClassId = dtSearch.Rows[0]["ClassId"].ToString();
            dtSearch.Clear();
            dtSearch.Dispose();
            TemplateEngine te = new TemplateEngine(doh, _ChannelId);
            doh.Reset();
            doh.SqlCmd = "SELECT Id FROM [jcms_normal_class] WHERE [IsOut]=0 AND [ChannelId]=" + _ChannelId + " and [Id]=" + ClassId;
            if (doh.GetDataTable().Rows.Count == 0)
            {
                return "";
            }
            string PageStr = string.Empty;
            doh.Reset();
            doh.SqlCmd = "SELECT * FROM [jcms_module_photo] WHERE [ChannelId]=" + _ChannelId + " and [IsPass]=1 and [Id]=" + _ContentId;
            DataTable dtContent = doh.GetDataTable();
            System.Collections.ArrayList ContentList = new System.Collections.ArrayList();
            p__GetChannel_Photo(doh, te, dtContent, ref PageStr, ref ContentList, 0);
            te.ReplaceContentTag(ref PageStr, _ContentId);
            ContentList.Add(PageStr);
            p__replaceSinglePhoto(doh, dtContent, ref PageStr, ref ContentList);

            dtContent.Clear();
            dtContent.Dispose();
            if (_CurrentPage < 1 || _CurrentPage > (ContentList.Count))
                _CurrentPage = 1;
            return ContentList[0].ToString().Replace("{$Content}", ContentList[_CurrentPage].ToString()).Replace("{$_getPageBar()}", getPageBar(0, "html", 2, ContentList.Count - 1, 1, _CurrentPage, Go2View(doh, -1, false, _ChannelId, _ContentId)));
        }
        private void p__GetChannel_Photo(DbOperHandler doh, TemplateEngine te, DataTable dt, ref string PageStr, ref System.Collections.ArrayList ContentList, int i)
        {
            doh.Reset();
            doh.ConditionExpress = "IsOut=0 AND id=" + dt.Rows[i]["ClassId"].ToString();
            string TempId = doh.GetValue("jcms_normal_class", "ContentTemp").ToString();
            string ClassName = doh.GetValue("jcms_normal_class", "Title").ToString();
            string pId = string.Empty;
            string sId = string.Empty;
            doh.Reset();
            doh.SqlCmd = "SELECT TOP 1 [pid],[Source],[SkinId] FROM [jcms_normal_template] WHERE [Id]=" + TempId;
            DataTable dtTemplate = doh.GetDataTable();
            if (dtTemplate.Rows.Count > 0)
            {
                pId = dtTemplate.Rows[0]["pid"].ToString();
                sId = dtTemplate.Rows[0]["SkinId"].ToString();
                TemplateProject tp = new TemplateProject();
                PageStr = Jumbot.Common.DirFile.ReadFile(site.Dir + "templates/" + tp.GetDir(pId, doh) + "/" + dtTemplate.Rows[0]["Source"].ToString());
            }
            dtTemplate.Clear();
            dtTemplate.Dispose();
            te.PageNav = "<script type=\"text/javascript\" src=\"" + site.Dir + te.Channel.Dir + "/js/classnav_" + dt.Rows[i]["ClassId"].ToString() + ".js\"></script>&nbsp;&gt;&nbsp;内容";
            te.PageTitle = dt.Rows[i]["Title"] + "_" + te.Channel.Title + "_" + site.Name + site.TitleTail;
            te.PageKeywords =  Jumbot.Common.WordSpliter.GetKeyword(dt.Rows[i]["Title"].ToString(), "~/_data/default.dic") + "," + site.Keywords;
            te.PageDescription = Jumbot.Common.Strings.SimpleLineSummary(dt.Rows[i]["Summary"].ToString());
            te.ReplacePublicTag(ref PageStr, pId, sId);
            te.ReplaceChannelTag(ref PageStr, dt.Rows[i]["ChannelId"].ToString());
            te.ReplaceClassTag(ref PageStr, dt.Rows[i]["ClassId"].ToString());
            te.ReplaceRepeatTag(ref PageStr);
        }

        private void p__replaceSinglePhoto(DbOperHandler doh, DataTable dt, ref string PageStr, ref System.Collections.ArrayList ContentList)
        {
            //图片地址分割处理
            string PhotoUrl = dt.Rows[0]["PhotoUrl"].ToString().Replace("\r\n", "\r").Replace("|||", "\r");
            string[] PhotoUrlArr = PhotoUrl.Split(new string[] { "\r" }, StringSplitOptions.RemoveEmptyEntries);
            int PSize = Str2Int(dt.Rows[0]["PageSize"].ToString(), 1);
            string TempStr = string.Empty;
            if (PhotoUrlArr.Length >= PSize)
            {
                bool isChange = true;
                int pageCount = 1;
                if (PhotoUrlArr.Length % PSize == 0)
                    pageCount = PhotoUrlArr.Length / PSize;
                else
                    pageCount = (PhotoUrlArr.Length / PSize) + 1;
                for (int j = 0; j < PhotoUrlArr.Length; j++)
                {
                    if (isChange)
                        TempStr = "<br><img src='" + PhotoUrlArr[j] + "'><br />";
                    else
                        TempStr += "<br><img src='" + PhotoUrlArr[j] + "'><br />";

                    if ((j + 1) % PSize == 0)
                    {
                        ContentList.Add(TempStr);
                        isChange = true;
                    }
                    else
                    {
                        isChange = false;
                        if (j == PhotoUrlArr.Length - 1)
                            ContentList.Add(TempStr);
                    }
                }
            }
            else
            {
                for (int j = 0; j < PhotoUrlArr.Length; j++)
                    TempStr += "<br><img src='" + PhotoUrlArr[j] + "'><br />";
                ContentList.Add(TempStr);
            }
        }
    }
}
